<template>
	<view>
		<button @click="searchBle">搜索蓝牙</button>
		 连接状态：<text> {{textValue}}</text>
		<view style="margin-top: 2upx;" :key="index" v-for="(item,index) in devices">
			<button style="  color: #0081FF;" @click="onConn(item)">{{item.name}}</button>
		</view>
		<button style="margin-top: 100upx;" @click="senBleLabel()">标签打印</button>
		<textarea @blur="piaojuText" auto-height placeholder-style="color:#F76260" placeholder="请输入票据信息" v-model="piaojuText" />
		<button style="margin-top: 100upx;" @click="senBleLabel2()">票据打印</button>
	</view>


</template>

<script>
	
	var main = plus.android.runtimeMainActivity();
	// 加载插件 获取 module 
	var blePlatform = uni.requireNativePlugin("TH-PlatformBLE")//新增方法  引用插件
	
	export default {
		data() {
			return {
				devices: [],
				currDev: null,
				connId: '',
				piaojuText:'',
				 textValue: '',
				 // 标签模板
				 templateType: "40*30",
				 countPrint:0,
			}
		},
		onLoad: function() {
		
		},

	onShow: function(){
			var that = this
			blePlatform.connState((ret) => {//新增方法 (蓝牙连接的状态回调)
					console.log(""+ret)
					this.textValue =""+ret;
					if(ret == 0){
						//连接成功 关闭搜索
						that.stopFindBule()
						console.log("stopFindBule()-------")
					}
				});
			
	
	},
		methods: {
			destroyed: function() {
				console.log("destroyed----------")
				if (this.connId != '') {
					uni.closeBLEConnection({
						deviceId: this.connId,
						success(res) {
							console.log(res)
						}
					})
				}

			},
			searchBle() {
				var that = this
				console.log("initBule")
				uni.openBluetoothAdapter({
					success(res) {
						console.log("打开 蓝牙模块")
						console.log(res)
						that.onDevice()
						uni.getBluetoothAdapterState({
							success: function(res) {
								console.log(res)
								if (res.available) {
									if (res.discovering) {
										that.stopFindBule()
									}
									//搜索蓝牙
									//开始搜寻附近的蓝牙外围设备
									console.log("开始搜寻附近的蓝牙外围设备")
									uni.startBluetoothDevicesDiscovery({
										success(res) {
											console.log(res)
										}
									})

								} else {
									console.log('本机蓝牙不可用')
								}
							},
						})
					}
				})
			},
			onDevice(){
				console.log("监听寻找到新设备的事件---------------")
				var that = this
				//监听寻找到新设备的事件
				uni.onBluetoothDeviceFound(function(devices) {
							console.log('--------------new-----------------------'+JSON.stringify(devices))
							var re = JSON.parse(JSON.stringify(devices))
							console.log(re.devices[0].name + "  " + re.devices[0].deviceId)
								let name = re.devices[0].name
								
								if (name != "未知设备" && name != "" && name != "null") {
									let deviceId = re.devices[0].deviceId
									that.devices.push({
										name: name,
										deviceId: deviceId,
										services: []
									})
								}

				})
			},
			
			stopFindBule() {
				console.log("停止搜寻附近的蓝牙外围设备---------------")
				uni.stopBluetoothDevicesDiscovery({
					success(res) {
						console.log(res)
					}
				})
			},
			onConn(item) {
				var that = this
				console.log("连接蓝牙---------------" + item.deviceId)
				let deviceId = item.deviceId
				blePlatform.bluetoothConn(deviceId);//新增方法 开始连接蓝牙，下面的原生连接已注释
				
				console.log("连接蓝牙-[" + item.name + "]-")
				// uni.createBLEConnection({
				// 	deviceId: deviceId,
				// 	complete(res) {
				// 		if (res.errMsg == "createBLEConnection:ok") {
				// 			console.log("连接蓝牙-[" + item.name + "]--成功")
				// 			that.connId = deviceId;
				// 			that.currDev = item
				// 			setTimeout(function() {
				// 				that.getBLEServices(deviceId)
				// 			}, 2000)
				// 		} else {
				// 			console.log(res)
				// 		}
				// 		//连接成功 关闭搜索
				// 		that.stopFindBule()

				// 	},
				// })
				
				
				
			},

			getBLEServices(_deviceId) {
				var that = this;
				let deviceId = _deviceId
				console.log("获取蓝牙设备所有服务(service)。---------------")

				uni.getBLEDeviceServices({
					// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
					deviceId: deviceId,
					complete(res) {
						console.log(res)
						let serviceId = ""

						for (var s = 0; s < res.services.length; s++) {
							console.log(res.services[s].uuid)
							let serviceId = res.services[s].uuid
							uni.getBLEDeviceCharacteristics({
								// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
								deviceId: deviceId,
								// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
								serviceId: serviceId,
								success(res) {
									var re = JSON.parse(JSON.stringify(res))

									console.log('deviceId = [' + deviceId + ']  serviceId = [' + serviceId + ']')
									for (var c = 0; c < re.characteristics.length; c++) {
										if (re.characteristics[c].properties.write == true) {
											let uuid = re.characteristics[c].uuid
											console.log(' deviceId = [' + deviceId + ']  serviceId = [' + serviceId + '] characteristics=[' +
												uuid)

											for (var index in that.devices) {
												if (that.devices[index].deviceId == deviceId) {
													that.devices[index].services.push({
														serviceId: serviceId,
														characteristicId: uuid
													})
													break
												}

											}
											console.log(JSON.stringify(that.devices))

										}
									}
								}
							})

						}



					},
					fail(res) {
						console.log(res)
					},
				})

			},
			senBlData(deviceId, serviceId, characteristicId,uint8Array) {
				console.log('************deviceId = [' + deviceId + ']  serviceId = [' + serviceId + '] characteristics=[' +characteristicId+ "]")
				var uint8Buf = Array.from(uint8Array);
				function split_array(datas,size){
					var result = {};
					var j = 0
					for (var i = 0; i < datas.length; i += size) {
						result[j] = datas.slice(i, i + size)
						j++
					}
					console.log("split_array() result:"+result)
					return result
				}
				var sendloop = split_array(uint8Buf, 20);
				// console.log(sendloop.length)
				function realWriteData(sendloop, i) {
					var data = sendloop[i]
					if(typeof(data) == "undefined"){
						return
					}
					console.log("第【" + i + "】次写数据"+data)
					var buffer = new ArrayBuffer(data.length)
					var dataView = new DataView(buffer)
					for (var j = 0; j < data.length; j++) {
						dataView.setUint8(j, data[j]);
					}
					uni.writeBLECharacteristicValue({
						deviceId,
						serviceId,
						characteristicId,
						value: buffer,
						success(res) {
							realWriteData(sendloop, i + 1);
						}
					})
				}
               var i = 0;
				realWriteData(sendloop, i);
			},
			
			senBleLabel() {
				this.countPrint =0;
				this.senBleLabelFor();
				},
			senBleLabelFor() {
				let that = this
				this.countPrint ++
				
				let data = {
								            //model：数据
								            code: "QR1234567890ABCDEF",
											materialName: "贴片电阻/100R/%0402",
											materialCode: "123456789",
											supplierName: "容奥",
											mfgTime: "2023-06-06",
											qty: "1205",
											po: "4500014628",
											supplierBatch: "20230815-S001133",
											
								        }
							//标签模式
							console.log("打印1", data)
							
								let canvasWidth = 40
								let canvasHeight = 50
								let {
									toLine,
									splitStr,
									notNullData
								} = that
								blePlatform.clear()//新增方法 清除数据
								blePlatform.setSize(40, 33)
								blePlatform.setGap(0)
								blePlatform.setCls()
								blePlatform.setBackFeed(0)
								let col = 0
								let font = "TSS16.BF2"
								blePlatform.setText(col, toLine(1), font, 1, 1, "物料编码\"\:测试")
								blePlatform.setQR(toLine(11), toLine(1), "L", 3, "A", notNullData(data.code))
								let codeStr = notNullData(data.code)
								let codeList = splitStr(codeStr, 11)
								for (let codeListIndex = 0; codeListIndex < codeList.length; codeListIndex++) {
									blePlatform.setText(toLine(11), toLine((5 + codeListIndex)), font, 1, 1, codeList[codeListIndex])
								}
								blePlatform.setText(col, toLine(2), font, 2, 2, notNullData(data.materialCode))
								blePlatform.setText(col, toLine(4), font, 1, 1, "物料名称:")
								let materialName = notNullData(data.materialName)
								let materialNameList = splitStr(materialName, 20)
								let i = 0
								for (; i < materialNameList.length; i++) {
									blePlatform.setText(col, toLine(5 + i), font, 1, 1, materialNameList[i])
								}
								blePlatform.setText(col, toLine(5 + i), font, 1, 1,  "供应商简称:" + notNullData(data.supplierName))
								blePlatform.setText(col, toLine(6 + i), font, 1, 1,  "供应商生产白期:" + notNullData(data.mfgTime))
								blePlatform.setText(toLine(11), toLine(6 + i), font, 1, 1, "数量:")
								blePlatform.setText(toLine(13), toLine(6 + i), font, 2, 2, notNullData(data.qty))
								blePlatform.setText(col, toLine(7 + i), font, 1, 1, "采购单号:" + notNullData(data.po))
								blePlatform.setText(col, toLine(8 + i), font, 1, 1, "供应商生产批次:" + notNullData(data.supplierBatch))
								console.log("打印2", blePlatform)
								blePlatform.setPagePrint()
								let ret = blePlatform.startPrint();//新增方法 开始打印
								console.log("打印2", "startPrint()"+ret)
								
								
								if(this.countPrint <1){//打印10次
								setTimeout(function() {
								 that.senBleLabelFor()
								}, 500); // 这里的 1000 表示延时的时间，单位是毫秒
								}
								
								// uni.canvasGetImageData({
								// 	canvasId: 'edit_area_canvas',
								// 	x: 0,
								// 	y: 0,
								// 	width: canvasWidth,
								// 	height: canvasHeight,
								// 	success: function(res) {
								// 		console.log("打印3", res)
								// 		// command.setBitmap(60, 0, 1, res)
								// 	},
								// 	complete: function() {
								// 		console.log("打印4", command)
								// 		command.setPagePrint()
								// 		that.prepareSend(command.getData())
								// 	}
								// })
			},// 标签 宽60 高80 横向
			labelTest60X80(data) {
				console.log("打印1", data)
				let that = this;
				let canvasWidth = that.canvasWidth
				let canvasHeight = that.canvasHeight
				let command =blePlatform
				let {
					toLine,
					splitStr,
					notNullData
				} = that
				command.setSize(60, 83)
				command.setGap(0)
				command.setCls()
				command.setBackFeed(0)
				let row = 20
				let font = "TSS24.BF2"
				command.setTextRotation(toLine(12), row, font, 90, 1, 1, "物料编码:");
				command.setTextRotation(toLine(11), row, font, 90, 2, 2, notNullData(data.materialCode))
				command.setTextRotation(toLine(9), row, font, 90, 1, 1, "物料名称:")
				let materialName = notNullData(data.materialName)
				let materialNameList = splitStr(materialName, 20)
				let i = 0
				for (; i < materialNameList.length; i++) {
					command.setTextRotation(toLine(8 - i), row, font, 90, 1, 1, materialNameList[i])
				}
				command.setTextRotation(toLine(8 - i), row, font, 90, 1, 1, "供应商简称:" + notNullData(data
					.supplierName))
				command.setTextRotation(toLine(7 - i), row, font, 90, 1, 1, "供应商生产白期:" + notNullData(data.mfgTime))
				command.setTextRotation(toLine(7 - i), toLine(10), font, 90, 1, 1,"数量:")
				command.setTextRotation(toLine(7 - i), toLine(12), font, 90, 2, 2, notNullData(data.qty))
				command.setTextRotation(toLine(6 - i), row, font, 90, 1, 1, "采购单号:" + notNullData(data.po))
				command.setTextRotation(toLine(5 - i), row, font, 90, 1, 1, "供应商生产批次:" + + notNullData(data
					.supplierBatch))
				command.setQRRotation(toLine(12), toLine(12), "L", 5, "A", 90, notNullData(data.code))
				let codeStr = notNullData(data.code)
				let codeList = splitStr(codeStr, 11)
				for (let codeListIndex = 0; codeListIndex < codeList.length; codeListIndex++) {
					command.setTextRotation(toLine((9 - codeListIndex)), toLine(12), font, 90, 1, 1, codeList[
						codeListIndex])
				}

				console.log("打印2", command)
				uni.canvasGetImageData({
					canvasId: 'edit_area_canvas',
					x: 0,
					y: 0,
					width: canvasWidth,
					height: canvasHeight,
					success: function(res) {
						console.log("打印3", res)
						// command.setBitmap(60, 0, 1, res)
					},
					complete: function() {
						console.log("打印4", command)
						command.setPagePrint()
						that.prepareSend(command.getData())
					}
				})
			},
			// 获取非空数据
						notNullData(data) {
							return (data ? data : data == "0" ? 0 : "")
						},
						// 文本分割
									splitStr(str, len) {
										let reg = new RegExp(".{" + len + "}", 'g');
										let list = str.match(reg)
										if (list == null) {
											list = []
										}
										// 尾数添加
										list.push(str.substr(list.length * len, str.length % len))
										return list
									},
						toLine(index) {
										let that = this
										let lineHeight = 30
										if (that.templateType == '80*60') {
											lineHeight = 30
										} else if (that.templateType == '40*30') {
											lineHeight = 30 * 0.6
										} else if (that.templateType == '60*80') {
											lineHeight = 30 * 1.2
										} else if (that.templateType == '60*40') {
											lineHeight = 30 * 1
										}
										return lineHeight * index
									},
						
						
			senBleLabel2(){
				//票据模式
				let deviceId = this.currDev.deviceId;
				let serviceId = this.currDev.services[0].serviceId;
				let characteristicId = this.currDev.services[0].characteristicId;
				var command = esc.jpPrinter.createNew()
				command.init()
                command.setText(this.piaojuText);
                command.setPrintAndFeedRow(1)
				this.senBlData(deviceId, serviceId, characteristicId,command.getData())
			}
		}
	}
</script>

<style>

</style>
